package onjava.arrays;

// arrays/IceCreamFlavors.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Returning arrays from methods
import java.util.*;
import static onjava.ArrayShow.*;
import static onjava.Consts.*;
/**
 * 
 * 从方法中返回数组
 *
 * @author Lucky Yang
 */
public class IceCreamFlavors {
  private static SplittableRandom rand = new SplittableRandom(47);
  /** 所有的口味列表 */
  static final String[] FLAVORS = {
      "Chocolate", "Strawberry", "Vanilla Fudge Swirl",
      "Mint Chip", "Mocha Almond Fudge", "Rum Raisin",
      "Praline Cream", "Mud Pie"
  };

  /**
   * 设置口味。随机选择n钟口味，保证不会选到已被选过的
   * 
   * @param n 口味数量
   * @return
   */
  public static String[] flavorSet(int n) {
    if (n > FLAVORS.length) {
      throw new IllegalArgumentException("Set too big");
    }
    String[] results = new String[n];
    boolean[] picked = new boolean[FLAVORS.length];
    for (int i = 0; i < n; i++) {
      int t;
      do
        t = rand.nextInt(FLAVORS.length);
      while (picked[t]);
      results[i] = FLAVORS[t];
      picked[t] = true;
    }
    return results;
  }

  public static void main(String[] args) {
    for (int i = 0; i < SEVEN; i++) {
      show(flavorSet(3));
    }
  }
}
/*
 * Output:
 * [Praline Cream, Mint Chip, Vanilla Fudge Swirl]
 * [Strawberry, Vanilla Fudge Swirl, Mud Pie]
 * [Chocolate, Strawberry, Vanilla Fudge Swirl]
 * [Rum Raisin, Praline Cream, Chocolate]
 * [Mint Chip, Rum Raisin, Mocha Almond Fudge]
 * [Mocha Almond Fudge, Mud Pie, Vanilla Fudge Swirl]
 * [Mocha Almond Fudge, Mud Pie, Mint Chip]
 */
